package cn.iocoder.yudao.module.biz.service.htPlanDetail;

import cn.iocoder.yudao.module.biz.controller.admin.htPlan.vo.HtPlanPageReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.htPlan.vo.HtPlanRiskVO;
import cn.iocoder.yudao.module.biz.dal.mysql.htPlan.HtPlanMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.biz.controller.admin.htPlanDetail.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.htPlanDetail.HtPlanDetailDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.biz.convert.htPlanDetail.HtPlanDetailConvert;
import cn.iocoder.yudao.module.biz.dal.mysql.htPlanDetail.HtPlanDetailMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 隐患排查计划详情 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class HtPlanDetailServiceImpl implements HtPlanDetailService {

    @Resource
    private HtPlanDetailMapper htPlanDetailMapper;
    @Resource
    private HtPlanMapper htPlanMapper;

    @Override
    public String createHtPlanDetail(HtPlanDetailCreateReqVO createReqVO) {
        // 插入
        HtPlanDetailDO htPlanDetail = HtPlanDetailConvert.INSTANCE.convert(createReqVO);
        htPlanDetailMapper.insert(htPlanDetail);
        // 返回
        return htPlanDetail.getId();
    }

    @Override
    public void updateHtPlanDetail(HtPlanDetailUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateHtPlanDetailExists(updateReqVO.getId());
        // 更新
        HtPlanDetailDO updateObj = HtPlanDetailConvert.INSTANCE.convert(updateReqVO);
        htPlanDetailMapper.updateById(updateObj);
    }

    @Override
    public void deleteHtPlanDetail(String id) {
        // 校验存在
        this.validateHtPlanDetailExists(id);
        // 删除
        htPlanDetailMapper.deleteById(id);
    }

    private void validateHtPlanDetailExists(String id) {
        if (htPlanDetailMapper.selectById(id) == null) {
            throw exception(HT_PLAN_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public HtPlanDetailDO getHtPlanDetail(String id) {
        return htPlanDetailMapper.selectById(id);
    }

    @Override
    public List<HtPlanDetailDO> getHtPlanDetailList(Collection<String> ids) {
        return htPlanDetailMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<HtPlanDetailDO> getHtPlanDetailPage(HtPlanDetailPageReqVO pageReqVO) {
        return htPlanDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public List<HtPlanDetailDO> getHtPlanDetailList(HtPlanDetailExportReqVO exportReqVO) {
        return htPlanDetailMapper.selectList(exportReqVO);
    }

    @Override
    public List<HtPlanRiskVO>  queryPlanDetailBy(HtPlanDetailPageReqVO pageVO) {
        return htPlanDetailMapper.queryByPlan(pageVO);
    }

    @Override
    public Integer queryPlanDetailByCount(HtPlanDetailPageReqVO pageVO) {
        return htPlanDetailMapper.queryByPlanCount(pageVO);
    }
}
